import os

import wx
from pubsub import pub

from 文档自动填写GUI程序.DrillThread import DrillThread
from 文档自动填写GUI程序.PerfusionThread import PerfusionThread
from 文档自动填写GUI程序.PrintThread import PrintThread
from 文档自动填写GUI程序.RatioThread import RatioThread
from 文档自动填写GUI程序.RecordThread import RecordThread
from 文档自动填写GUI程序.common import split_string_by_multiple_delimiters


class MyFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, title=title, size=(700, 600))
        self.SetMaxSize((700, 600))
        self.panel = wx.Panel(self)
        wx.StaticText(self.panel, label="桩基号:", pos=(50, 35), style=wx.ALIGN_CENTRE_HORIZONTAL)

        star_label = wx.StaticText(self.panel, style=wx.ALIGN_CENTER, pos=(40, 38), size=(5, -1))
        star_label.SetForegroundColour("red")
        star_label.SetLabel("*")

        # 桩基号输入框
        self.input = wx.TextCtrl(self.panel, pos=(100, 30), size=(300, 120), style=wx.TE_MULTILINE)
        self.input.SetHint("请输入桩基号，多个用回车，分号分割，不可以为空")

        wx.StaticText(self.panel, label="充盈系数:", pos=(40, 180), style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.ratio = wx.TextCtrl(self.panel, pos=(100, 174), size=(300, 30), style=0)
        self.ratio.SetHint("充盈系数多个用分号或逗号分割，可以为空")

        wx.StaticText(self.panel, label="灌注速度:", pos=(40, 220), style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.speed = wx.TextCtrl(self.panel, pos=(100, 214), size=(100, 28), style=0)
        self.speed.SetHint("30~50 m3/h")

        wx.StaticText(self.panel, label="拆管最大间隔:", pos=(220, 220), style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.dis_interval = wx.TextCtrl(self.panel, pos=(303, 214), size=(98, 28), style=0)
        self.dis_interval.SetHint("默认12分钟")

        wx.StaticText(self.panel, label="钻进速度:", pos=(40, 270), style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.drilling_speed = wx.TextCtrl(self.panel, pos=(100, 264), size=(100, 28), style=0)
        self.drilling_speed.SetHint("m/h(默认9m/h)")

        wx.StaticText(self.panel, label="钻进间隔:", pos=(220, 270), style=wx.ALIGN_CENTRE_HORIZONTAL)
        self.drilling_interval = wx.TextCtrl(self.panel, pos=(303, 264), size=(98, 28), style=0)
        self.drilling_interval.SetHint("分钟")

        self.record = wx.Button(self.panel, label="成孔检查记录生成", pos=(500, 30), size=(120, 30))
        self.drill = wx.Button(self.panel, label="钻进数据填写", pos=(500, 80), size=(120, 30))
        self.perfusion = wx.Button(self.panel, label="灌注数据填写", pos=(500, 130), size=(120, 30))
        # self.print = wx.Button(self.panel, label="打印", pos=(500, 180), size=(120, 30))
        self.info = wx.TextCtrl(self.panel, pos=(50, 330), size=(500, 180),
                                style=wx.TE_MULTILINE | wx.TE_WORDWRAP | wx.TE_READONLY)
        rgb_color = wx.Colour(242, 242, 254)
        self.info.SetBackgroundColour(rgb_color)
        self.Bind(wx.EVT_BUTTON, self.on_button_drill, self.drill)
        self.Bind(wx.EVT_BUTTON, self.on_button_record, self.record)
        self.Bind(wx.EVT_BUTTON, self.on_button_perfusion, self.perfusion)

        # 响应打印
        # self.Bind(wx.EVT_BUTTON, self.on_button_print, self.print)

        # 订阅消息
        pub.subscribe(self.on_info, 'info_status')

        pub.subscribe(self.on_error, 'error_status')

        # self.input.SetValue("1-Y-4")
        # self.ratio.SetValue("1.03")
        self.Show(True)

    # 检查记录
    def on_button_record(self, event):
        if os.path.exists("./out"):
            pass
        else:
            os.mkdir("out")
        if self.input.GetValue() == "" or self.input.GetValue() is None:
            wx.MessageBox("桩基编号不能为空")
        else:
            self.info.SetValue("")
            piler_no = self.input.GetValue()
            path = "./00桩基数据.xlsx"
            recordThread = RecordThread(path, piler_no)
            recordThread.start()

    # 钻进数据
    def on_button_drill(self, event):
        if os.path.exists("./out"):
            pass
        else:
            os.mkdir("out")

        if self.input.GetValue() == "" or self.input.GetValue() is None:
            wx.MessageBox("桩基编号不能为空")
        else:
            self.info.SetValue("")
            piler_no = self.input.GetValue()
            path = "./00桩基数据.xlsx"
            drilling_speed_text = self.drilling_speed.GetValue()
            if drilling_speed_text != "" and drilling_speed_text is not None:
                speed = int(drilling_speed_text)
            else:
                speed = 9
            drilling_interval_text = self.drilling_interval.GetValue()
            if drilling_interval_text != "" and drilling_interval_text is not None:
                interval = int(drilling_interval_text)
            else:
                interval = 4
            drillThread = DrillThread(path, piler_no, speed, interval)
            drillThread.start()

    # 灌注数据与拆管数据填写
    def on_button_perfusion(self, event):
        if os.path.exists("./out"):
            pass
        else:
            os.mkdir("out")
        if self.input.GetValue() == "" or self.input.GetValue() is None:
            wx.MessageBox("桩基编号不能为空")
        else:
            ratio = self.ratio.GetValue()
            if ratio == "" or ratio is None:
                self.info.SetValue("")
                piler_no = self.input.GetValue()
                path = "./00桩基数据.xlsx"
                interval_text = self.dis_interval.GetValue()
                if interval_text != "" and interval_text is not None:
                    interval = int(interval_text)
                else:
                    interval = 12
                speed_text = self.speed.GetValue()
                if speed_text != "" and speed_text is not None:
                    speed = int(speed_text)
                else:
                    speed = 40
                perfusionThread = PerfusionThread(path, piler_no, interval, speed)
                perfusionThread.start()
            else:
                piler_no = self.input.GetValue()
                path = "./00桩基数据.xlsx"
                ratioThread = RatioThread(path, piler_no, ratio)
                ratioThread.start()

    # 检查记录消息响应
    def on_info(self, message):
        text = message
        self.info.AppendText(text + "\n")

    def on_error(self, message):
        self.info.SetValue(str(message))

    def on_button_print(self, event):
        if os.path.exists("./out"):
            pass
        else:
            os.mkdir("out")
        if self.input.GetValue() == "" or self.input.GetValue() is None:
            wx.MessageBox("桩基编号不能为空")
            return
        delimiters = [',', ';', '，', '；', '\n']
        no = self.input.GetValue()
        piler_no_list = split_string_by_multiple_delimiters(no, delimiters)
        if len(piler_no_list) > 1:
            wx.MessageBox("一次只能打印一个桩基数据")
        else:
            dlg = wx.MessageDialog(None, '您确定要执行此操作吗？', '确认对话框',
                                   wx.YES_NO | wx.ICON_INFORMATION)
            result = dlg.ShowModal()
            if result == wx.ID_YES:
                printThread = PrintThread("./out", no)
                printThread.start()
            else:
                self.info.SetValue("")
            dlg.Destroy()


if __name__ == '__main__':
    app = wx.App(False)
    icon = wx.Icon('app.ico', wx.BITMAP_TYPE_ICO)
    frame = MyFrame(None, '桩基资料填写程序V1.0')
    frame.SetIcon(icon)
    app.MainLoop()
